package com.sailing.xjpb.ipaas.client.core.impl;

import com.sailing.xjpb.common.SailingConstants;
import com.sailing.xjpb.ipaas.client.core.IpaasClientService;
import com.sailing.xjpb.ipaas.client.core.IpaasHttpClient;
import com.sailing.xjpb.ipaas.client.entity.dto.SipOrganizationDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipRoleDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipUserCurrentInfoDTO;
import lombok.Data;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpMethod;

import java.util.List;

/**
 * ipaas client业务实现类
 *
 * @author YW
 */
@Data
public class IpaasClientServiceImpl implements IpaasClientService {

    private IpaasHttpClient ipaasHttpClient;

    @Override
    public boolean checkToken(String authorization) {
        String url = "/auth-server/oauth/get/token?ipaas_token=" + authorization;
        ipaasHttpClient.list(url, HttpMethod.POST, authorization, String.class);
        return true;
    }

    @Override
    @Cacheable(value = SailingConstants.CACHE_IPAAS_USER, key = "'SIP_USER_CURRENT_INFO_'+ #authorization")
    public SipUserCurrentInfoDTO getSipUserCurrentInfo(String authorization) {
        String url = "/external-interface/findCurrentUser";
        return ipaasHttpClient.exchange(url, HttpMethod.GET, authorization, SipUserCurrentInfoDTO.class);
    }

    @Override
    @Cacheable(value = SailingConstants.CACHE_IPAAS_USER, key = "'USER_ROLES_'+#authorization")
    public List<SipRoleDTO> listRoles(String authorization, String userName) {
        String url = "/external-interface/userContext/findRoles/" + userName;
        return ipaasHttpClient.list(url, HttpMethod.GET, authorization, SipRoleDTO.class);
    }

    @Override
    @Cacheable(value = SailingConstants.CACHE_IPAAS_ORG, key = "'GET_ORG_BY_ID_'+ #orgId")
    public SipOrganizationDTO getOrgById(String orgId) {
        String url = "/external-interface/rmAuca/findOrg/" + orgId;
        return ipaasHttpClient.exchange(url, HttpMethod.GET, SipOrganizationDTO.class);
    }

}
